Author

Matthew Chen

Loading in packages and the dataset

Code
library(tidyverse)
library(dplyr)
library(glue)
library(stringr)
library(purrr)
library(english)
library(here)
library(moderndive)
library(DT)
library(gt)
name<-read_csv(here("week 9", "StateNames_A.csv"))

Part 3 Interactive table

Code
DT::datatable(name)

Part 1 and part 2

Question 1 table

Code
Q1_table<-name|>
  rename(sex = Gender)|>
  filter(Name == "Allison")|>
  group_by(sex,State)|>
  summarise(Name = n(), .groups = 'drop')|>
  pivot_wider(names_from = sex, values_from = Name, values_fill = 0)

Q1_table|>
  gt()|>
  cols_label(F = md("**Female**"), M = md("**Male**"), State = md("**State**")) |>
  tab_header(title = md("**Number of Allison**"),
             subtitle =md("**for each state and sex**"))|>
  as_raw_html()
Number of Allison
for each state and sex
State Female Male
AK 18 0
AL 18 0
AR 18 0
AZ 18 0
CA 18 0
CO 18 0
CT 18 0
DC 18 0
DE 18 0
FL 18 0
GA 18 0
HI 17 0
IA 18 0
ID 18 0
IL 18 0
IN 18 0
KS 18 0
KY 18 1
LA 18 0
MA 18 0
MD 18 0
ME 18 0
MI 18 0
MN 18 0
MO 18 0
MS 18 0
MT 18 0
NC 18 0
ND 18 0
NE 18 0
NH 18 0
NJ 18 0
NM 18 0
NV 18 0
NY 18 0
OH 18 0
OK 18 0
OR 18 0
PA 18 0
RI 18 0
SC 18 0
SD 18 0
TN 18 0
TX 18 0
UT 18 0
VA 18 0
VT 14 0
WA 18 0
WI 18 0
WV 18 0
WY 18 0

Question 5 table

Code
allison_f<-name|>
  filter(Name == "Allison", Gender =="F")

allison_data<-allison_f|>
  group_by(Year)|>
  summarise(sum(Count))|>
  rename(N_of_Allison = "sum(Count)")

allison_lm<-lm(N_of_Allison~Year, data = allison_data)

Q5_table<-get_regression_table(allison_lm)

Q5_table|>
  gt()|>
  tab_header(title = md("**Regression Table for *Number of Allison***"))|>
  tab_options(column_labels.background.color = "green")|>
  as_raw_html()
Regression Table for Number of Allison
term estimate std_error statistic p_value lower_ci upper_ci
intercept 209689.761 42971.505 4.880 0 118594.240 300785.282
Year -101.519 21.427 -4.738 0 -146.942 -56.096

Question 9 table:

Code
male_name<-name|>
  filter(Name%in% c("Allan","Alan", "Allen"), Gender == "M")

Q9_table<-male_name|>
  filter(Year == 2000, State %in%c("PA","CA"))|>
  select(Name,State,Count)|>
  pivot_wider(names_from = Name, values_from = Count )

Q9_table|>
  gt()|>
  tab_header(title = md("**Frequency of the three different spelling of *Allan* in California and Pennsylvania**"))|>
  tab_options(column_labels.background.color = "red")|>
  tab_style(style = cell_borders(color = "blue", sides = c("left", "right")), locations = cells_body(columns = State))|>
  as_raw_html()
Frequency of the three different spelling of Allan in California and Pennsylvania
State Alan Allen Allan
CA 579 176 131
PA 51 56 12

Question 10 table:

Code
Q10_table<-male_name|>
  filter(Year == 2000, State %in%c("PA","CA"))|>
  select(Name,State,Count)|>
  group_by(State)|>
  mutate(percent = (Count/sum(Count)))|>
  ungroup()|>
  select(!Count)|>
  pivot_wider(names_from = Name, values_from = percent)

Q10_table|>
  gt()|>
  tab_header(title = md("**Percentages of the three different spelling of *Allan* in California and Pennsylvania**"))|>
  fmt_percent(columns = 2:4, decimals = 1)|>
 tab_options(column_labels.background.color = "blue")|>
  tab_style(style = cell_borders(color = "red", sides = c("left", "right")), locations = cells_body(columns = State))|>
  as_raw_html()
Percentages of the three different spelling of Allan in California and Pennsylvania
State Alan Allen Allan
CA 65.3% 19.9% 14.8%
PA 42.9% 47.1% 10.1%

Sources:

  1. https://gt.rstudio.com/reference/cell_borders.html
  2. https://gt.rstudio.com/reference/as_raw_html.html
  3. https://stackoverflow.com/questions/48446816/explain-ungroup-in-dplyr
  4. https://gt.rstudio.com/reference/tab_options.html